package ace.cmp.spring.data.jdbc.querydsl.core.repository.handler.impl;

import ace.cmp.spring.data.jdbc.querydsl.core.model.PropertyRelation;
import ace.cmp.spring.data.jdbc.querydsl.core.repository.handler.QuerydslNowGenerator;
import java.sql.Timestamp;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.Date;

/**
 * @author caspar
 * @date 2024/6/27 10:57
 */
public class QuerydslNowGeneratorImpl implements QuerydslNowGenerator {
  @Override
  public Object generate(PropertyRelation propertyRelation) {
    String typeName = propertyRelation.getField().getGenericType().getTypeName();
    if (Long.class.getTypeName().equals(typeName)) {
      return Instant.now().toEpochMilli();
    } else if (Integer.class.getTypeName().equals(typeName)) {
      return Long.valueOf(Instant.now().getEpochSecond()).intValue();
    } else if (LocalDateTime.class.getTypeName().equals(typeName)) {
      return LocalDateTime.now();
    } else if (LocalDate.class.getTypeName().equals(typeName)) {
      return LocalDate.now();
    } else if (LocalTime.class.getTypeName().equals(typeName)) {
      return LocalTime.now();
    } else if (Date.class.getTypeName().equals(typeName)) {
      return Date.from(Instant.now());
    } else if (Timestamp.class.getTypeName().equals(typeName)) {
      return Timestamp.valueOf(LocalDateTime.now());
    } else {
      return null;
    }
  }
}
